package server;

import org.ansj.splitWord.analysis.NlpAnalysis;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.*;

/**
 * Created by xjzhang on 2017/12/5.
 */
public class OutTermProcesser {



    Map<String,String> mapJiwai = new HashMap<>();



    public void index(String indexContentPath) throws Exception{
        try {
            String line = "";
            File file = new File(indexContentPath);
            if(file.exists()){
                File[] files = file.listFiles();
                for (File oneFile : files){
                    String command = oneFile.getName();
                    command = command.replace(".txt","");
                    InputStreamReader inputStreamReader= new InputStreamReader(new FileInputStream(oneFile),"utf-8");
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    while ((line = bufferedReader.readLine()) != null){
                        line = line.replaceAll("[\\pP\\p{Punct}]","");
                        mapJiwai.put(line,command);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String search(String queryStr,float nDistancePercentage){

        String ret ="";
        try {
            String tempt = queryStr.replaceAll("[\\pP\\p{Punct}]", "");
            DistanceCompute dc = new DistanceCompute();
            int ndistance;
            int matchLength;
            float accu = 0f;
            for (Map.Entry<String,String> entry : mapJiwai.entrySet()){
                if (entry.getKey().length()<= tempt.length()){
                    ndistance = dc.annotate( entry.getKey(),tempt);
                    matchLength = tempt.length()-ndistance;
                    accu = (float)matchLength/(float)tempt.length();
                }
                else {
                    ndistance = dc.annotate(tempt,entry.getKey());
                    matchLength = entry.getKey().length()-ndistance;
                    accu = (float)matchLength/(float)entry.getKey().length();
                }

                if (accu>nDistancePercentage){
                    ret = entry.getValue();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ret;
    }
}
